package com.lujieni.springbootjvm.classloader;

import com.lujieni.springbootjvm.po.Pool;

/**
 * 测试类加载器
 * 逐步向上找类加载器中的缓存有没有当前类,到了顶层之后还是没有再向下委派(这种机制是为了安全)
 * AppClassLoader -> ExtClassLoader -> null(Bootstrap 因为是由c++实现的所以java中没有对应类)
 */
public class TestClassLoader {

    public static void main(String[] args) throws ClassNotFoundException {
        test04();
    }

    public static void test01(){
        System.out.println(Pool.class.getClassLoader().getClass());
        System.out.println(Pool.class.getClassLoader().getParent());
        System.out.println(Pool.class.getClassLoader().getParent().getParent());
    }

    public static void test02(){
        String pathBoot = System.getProperty("sun.boot.class.path");
        System.out.println(pathBoot.replaceAll(";", System.lineSeparator()));

        System.out.println("-------------------");

        String pathExt = System.getProperty("java.ext.dirs");
        System.out.println(pathExt.replaceAll(";", System.lineSeparator()));
        System.out.println("-------------------");

        String pathApp = System.getProperty("java.class.path");
        System.out.println(pathApp.replaceAll(";", System.lineSeparator()));
    }

    public static void test03() throws ClassNotFoundException {
        System.out.println(TestClassLoader.class.getClassLoader());
        Class<?> clazz = TestClassLoader.class.getClassLoader().loadClass("com.lujieni.springbootjvm.classloader.TestClassLoader");
        System.out.println(clazz.getName());
    }

    /**
     * preparation: count=0 t=null  initializtion: count=2  new T() 导致 count++
     *
     * load Class: 默认值-初始值
     * new Class: 申请内存-默认值-初始值
     */
    public static void test04(){
        System.out.println(T.count);
    }




}

 class T{
     public static int count = 2;
     public static T t = new T();

    private T(){
        count++;
    }

}
